<?php

namespace app\admin\controller;
use think\Db;

class Coin extends Common{

	public function wallet(){
		return $this->fetch();
	}

	public function walletList(){
		$limit  = request()->param('limit')?:20;
		$data  = Db::table('tp_coin_wallet')
				->where('status',1)
				->order('sort asc')
				->paginate($limit);	
		$data = $data->toArray();
		foreach ($data['data'] as $key => $val) {
			$data['data'][$key]['icon'] = $this->imageUrl($val['icon']);
		}
		return $this->tableData($data);
	}
	public function coin(){
		if (request()->method() == 'POST') {

		} else {
			return $this->fetch();
		}
		return $this->fetch();
	}

	public function coinList(){
		$limit  = request()->param('limit')?:20;
		$data  = Db::table('tp_coin')->alias('c')
				->join('tp_coin_wallet w','w.id=c.wid')
				->where('c.status',1)
				->field('c.*,w.name wallet')
				->paginate($limit);	
		$data = $data->toArray();
		foreach ($data['data'] as $key => $val) {
			$data['data'][$key]['icon'] = $this->imageUrl($val['icon']);
		}
		return $this->tableData($data);
	}


	//兑换配置
	public function exConfig(){
		return $this->fetch();
	}
	public function exList(){
		$limit  = request()->param('limit')?:20;
		$data  = Db::table('tp_exchange_config')->alias('c')
				->join('tp_coin_wallet w1','w1.id=c.wid')
				->join('tp_coin_wallet w2','w2.id=c.to_wid')
				->field('c.*,w1.name from_wallet,w2.name to_wallet')
				->paginate($limit);	
		$data = $data->toArray();
		return $this->tableData($data);
	}

	public function exDel(){
		$id = request()->param('id');
		$res = Db::table('tp_exchange_config')->where('id',$id)->delete();
		if ($res) {
			return ['code'=>0,'msg'=>'successs'];
		} else {
			return ['code'=>1,'msg'=>'操作失败'];
		}
	}
	public function exEdit(){
		if (request()->method() == 'POST') {
			$data = request()->param();
			$id   = $data['id'];
			unset($data['id']);
			$data['fee_wid'] = $data['fee_wid']??$data['wid'];
			if ($id) {
				$res  = Db::table('tp_exchange_config')->where('id',$id)->update($data);
			} else {
				$has = Db::table('tp_exchange_config')->where('wid',$data['wid'])->where('to_wid',$data['to_wid'])->find();
				if ($has) {
					return ['code'=>1,'msg'=>'配置已存在'];
				}
				$res  = Db::table('tp_exchange_config')->insert($data);
			}
			if ($res) {
				return ['code'=>0,'msg'=>'success'];
			} else {
				return ['code'=>1,'msg'=>'更新失败'];
			}			
		} else {
			$id   = request()->param('id');
			$wallet = Db::table('tp_coin_wallet')
					->where('status',1)
					->select();
			$data = [];
			$data['wid'] = 0;
			$data['fee_type'] = 1;
			$data['to_wid'] = 0;
			if ($id) {
				$data = Db::table('tp_exchange_config')->alias('c')
							->join('tp_coin_wallet w1','w1.id=c.wid')
							->join('tp_coin_wallet w2','w2.id=c.to_wid')
							->where('c.id',$id)
							->field('c.*,w1.name from_wallet,w2.name to_wallet')
							->find();
			}
			$this->assign('data',$data);
			$this->assign('wallet',$wallet);
			return $this->fetch();
		}
	}



	//提币配置
	public function outConfig(){
		return $this->fetch();
	}
	public function outList(){
		$limit  = request()->param('limit')?:20;
		$data  = Db::table('tp_out_config')->alias('c')
				->join('tp_coin c1','c1.id=c.cid')
				->join('tp_coin_wallet w','w.id=c.wid')
				->field('c.*,w.name wallet,c1.name coin')
				->paginate($limit);	
		$data = $data->toArray();
		return $this->tableData($data);
	}

	public function outDel(){
		$id = request()->param('id');
		$res = Db::table('tp_out_config')->where('id',$id)->delete();
		if ($res) {
			return ['code'=>0,'msg'=>'successs'];
		} else {
			return ['code'=>1,'msg'=>'操作失败'];
		}
	}
		public function outEdit(){
		if (request()->method() == 'POST') {
			$data = request()->param();
			$id   = $data['id'];
			unset($data['id']);
			$data['wid'] = Db::table('tp_coin')->where('id',$data['cid'])->value('wid');
			$data['fee_wid'] = $data['fee_wid']??$data['wid'];
			if ($id) {
				$res  = Db::table('tp_out_config')->where('id',$id)->update($data);
			} else {
				$has = Db::table('tp_out_config')->where('cid',$data['cid'])->find();
				if ($has) {
					return ['code'=>1,'msg'=>'配置已存在'];
				}
				$res  = Db::table('tp_out_config')->insert($data);
			}
			if ($res) {
				return ['code'=>0,'msg'=>'success'];
			} else {
				return ['code'=>1,'msg'=>'更新失败'];
			}			
		} else {
			$id   = request()->param('id');
			$wallet = Db::table('tp_coin')->alias('c')
					->join('tp_coin_wallet w','w.id=c.wid')
					->where('c.status',1)
					->field('c.*,w.name wallet')
					->select();
			$data = [];
			$data['cid'] = 0;
			$data['fee_type'] = 1;
			if ($id) {
				$data = Db::table('tp_out_config')->alias('c')
							->join('tp_coin c1','c1.id=c.cid')
							->join('tp_coin_wallet w','w.id=c.wid')
							->where('c.id',$id)
							->field('c.*,w.name wallet,c1.name coin')
							->find();
			}
			$this->assign('data',$data);
			$this->assign('wallet',$wallet);
			return $this->fetch();
		}
	}



	//转账配置
	public function transConfig(){
		return $this->fetch();
	}
	public function transList(){
		$limit  = request()->param('limit')?:20;
		$data  = Db::table('tp_transfer_config')->alias('c')
				->join('tp_coin_wallet w','w.id=c.wid')
				->field('c.*,w.name wallet')
				->paginate($limit);	
		$data = $data->toArray();
		return $this->tableData($data);
	}

	public function transDel(){
		$id = request()->param('id');
		$res = Db::table('tp_transfer_config')->where('id',$id)->delete();
		if ($res) {
			return ['code'=>0,'msg'=>'successs'];
		} else {
			return ['code'=>1,'msg'=>'操作失败'];
		}
	}
	public function transEdit(){
		if (request()->method() == 'POST') {
			$data = request()->param();
			$id   = $data['id'];
			unset($data['id']);
			$data['fee_wid'] = $data['fee_wid']??$data['wid'];
			if ($id) {
				$res  = Db::table('tp_transfer_config')->where('id',$id)->update($data);
			} else {
				$has = Db::table('tp_transfer_config')->where('wid',$data['wid'])->find();
				if ($has) {
					return ['code'=>1,'msg'=>'配置已存在'];
				}
				$res  = Db::table('tp_transfer_config')->insert($data);
			}
			if ($res) {
				return ['code'=>0,'msg'=>'success'];
			} else {
				return ['code'=>1,'msg'=>'更新失败'];
			}			
		} else {
			$id   = request()->param('id');
			$wallet = Db::table('tp_coin_wallet')
					->where('status',1)
					->select();
			$data = [];
			$data['wid'] = 0;
			$data['fee_type'] = 1;
			if ($id) {
				$data = Db::table('tp_transfer_config')->alias('c')
							->join('tp_coin_wallet w','w.id=c.wid')
							->where('c.id',$id)
							->field('c.*,w.name wallet')
							->find();
			}
			$this->assign('data',$data);
			$this->assign('wallet',$wallet);
			return $this->fetch();
		}
	}
	/*
	修改表格数据
	*/
	public function editWallet(){
		if (request()->method() == 'POST') {
			$id  = request()->param('id');
			$field = request()->param('field');
			$value = request()->param('value');
			$huilv = C('huilv');
			$res = 1;
			if ($field == 'price') {
				if ($id >= 10) {
					$rmb  = $value*$huilv;
					$info = Db::table('tp_coin_wallet')->where('id',$id)->find();
					$rate = ($value - $info['price'])/$info['price'];
					$res  = Db::table('tp_coin_wallet')->where('id',$id)->update(['price'=>$value,'rate'=>$rate,'rmb'=>$rmb]);
					Db::table('tp_coin')->where('id',$id)->update(['price'=>$value,'rate'=>$rate,'rmb'=>$rmb]);
					$market = Db::table('tp_market')->where('cid',$id)->where('date','>=',strtotime(date('Y-m-d')))->find();
					if(!$market){
						Db::table('tp_market')->insert([
							'cid'=>$id,
							'price'=>$value,
							'date'=>strtotime(date('Y-m-d'))
						]);
					} else {
						Db::table('tp_market')->where('id',$market['id'])->update([
							'price'=>$value,
						]);
					}
				}
			} else if ($field == 'rmb'){
				if ($id >= 10) {
					$price = $value/$huilv;
					$info = Db::table('tp_coin_wallet')->where('id',$id)->find();
					$rate = ($price - $info['price'])/$info['price'];
					$res  = Db::table('tp_coin_wallet')->where('id',$id)->update(['price'=>$price,'rate'=>$rate,'rmb'=>$value]);
					Db::table('tp_coin')->where('id',$id)->update(['price'=>$price,'rate'=>$rate,'rmb'=>$value]);
					$market = Db::table('tp_market')->where('cid',$id)->where('date','>=',strtotime(date('Y-m-d')))->find();
					if(!$market){
						Db::table('tp_market')->insert([
							'cid'=>$id,
							'price'=>$price,
							'date'=>strtotime(date('Y-m-d'))
						]);
					} else {
						Db::table('tp_market')->where('id',$market['id'])->update([
							'price'=>$price,
						]);
					}
				}
				
			} else {
				$res  = Db::table('tp_coin')->where('id',$id)->update([$field=>$value]);
			}
			if ($res) {
				return ['code'=>0,'msg'=>'success'];
			} else {
				return ['code'=>1,'msg'=>'更新失败'];
			}			
		}
	}

	/*
	修改表格数据
	*/
	// public function editFee(){
	// 	if (request()->method() == 'POST') {
	// 		$id  = request()->param('id');
	// 		$field = request()->param('field');
	// 		$value = request()->param('value');
	// 		$usdt  = Db::table('tp_coin')->where('id',2)->find();
	// 		if ($field == 'price') {
	// 			$rmb  = $value*($usdt['rmb']/$usdt['price']);
	// 			$info = Db::table('tp_coin')->where('id',$id)->find();
	// 			$rate = ($value - $info['price'])/$info['price'];
	// 			$res  = Db::table('tp_coin')->where('id',$id)->update(['price'=>$value,'rate'=>$rate,'rmb'=>$rmb]);
	// 			$market = Db::table('tp_market')->where('cid',$id)->where('date','>=',strtotime(date('Y-m-d')))->find();
	// 			if(!$market){
	// 				Db::table('tp_market')->insert([
	// 					'cid'=>$id,
	// 					'price'=>$value,
	// 					'date'=>strtotime(date('Y-m-d'))
	// 				]);
	// 			} else {
	// 				Db::table('tp_market')->where('id',$market['id'])->update([
	// 					'price'=>$value,
	// 				]);
	// 			}
	// 		} else if ($field == 'rmb'){
	// 			$price = $value/($usdt['rmb']/$usdt['price']);
	// 			$info = Db::table('tp_coin')->where('id',$id)->find();
	// 			$rate = ($price - $info['price'])/$info['price'];
	// 			$res  = Db::table('tp_coin')->where('id',$id)->update(['price'=>$price,'rate'=>$rate,'rmb'=>$value]);
	// 			$market = Db::table('tp_market')->where('cid',$id)->where('date','>=',strtotime(date('Y-m-d')))->find();
	// 			if(!$market){
	// 				Db::table('tp_market')->insert([
	// 					'cid'=>$id,
	// 					'price'=>$price,
	// 					'date'=>strtotime(date('Y-m-d'))
	// 				]);
	// 			} else {
	// 				Db::table('tp_market')->where('id',$market['id'])->update([
	// 					'price'=>$price,
	// 				]);
	// 			}
	// 		} else {
	// 			$res  = Db::table('tp_coin')->where('id',$id)->update([$field=>$value]);
	// 		}
	// 		if ($res) {
	// 			return ['code'=>0,'msg'=>'success'];
	// 		} else {
	// 			return ['code'=>1,'msg'=>'更新失败'];
	// 		}			
	// 	}
	// }
}